package 剑指专题.链表;


/*
 * Author：江松
 * Date：2023/3/26 10:34
 *
 合并2个排序的链表：

 */

public class Main3 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    public class Solution {
        public ListNode Merge(ListNode list1,ListNode list2) {
            if(list1==null)return list2;
            if(list2==null)return list1;
            ListNode head=new ListNode(0);
            head.next=null;
            ListNode p=head;
            while(list1!=null&&list2!=null){
                if(list1.val<=list2.val){
                    p.next=list1;
                    list1=list1.next;
                }else{
                    p.next=list2;
                    list2=list2.next;
                }
                System.out.println(p.val);
                p=p.next;
            }
            p.next=list1!=null?list1:list2;
            return head.next;
        }
    }

    /*
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode head=new ListNode(0);
        //尾插,q多余了
        ListNode p,q=head;
        while (list1!=null&&list2!=null){
            if(list1.val<list2.val){
                p=list1;
                list1= list1.next;
            }else{
                p=list2;
                list2=list2.next;
            }
            q.next=p;
            if(p!=null)q=p;
        }
        //System.out.println(q.val);
        q.next=(list2==null)?list1:list2;
        return head.next;
    }
    */
}
